package cn.tanglong;

import org.apache.http.client.methods.RequestBuilder;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class EsTest {

        private TransportClient getClient() {
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true).build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }

    @Test
    public void addClient() throws UnknownHostException {
        // 创建索引
        TransportClient client = getClient();
        //
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "user", "1");
        // 添加数据
        Map<String, Object> data = new HashMap<>();
        data.put("id", 1L);
        data.put("username", "leilei");
        data.put("realname", "shuangshuang");
        data.put("sex", 1);
        data.put("age", 18);
        // 直接get结尾
        IndexResponse indexResponse = indexRequestBuilder.setSource(data).get();
        System.out.println(indexResponse);
    }

    /* 查询 */
    @Test
    public void get(){
        // 创建索引
        TransportClient client = getClient();
        // 获取索引值
        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "user", "1");
        GetResponse getFields = getRequestBuilder.get();
        System.out.println(getFields.getSource());

    }

    /* 修改 */
    @Test
    public void update(){
        // 创建索引
        TransportClient client = getClient();
        // 获取索引值
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "user", "1");
        Map<String, Object> data = new HashMap<>();
        data.put("id", 1L);
        data.put("username", "kaikai");
        data.put("realname", "shuangshuang");
        data.put("sex", 0);
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(data).get();
        System.out.println(updateResponse);
    }

    /* 删除 */
    @Test
    public void delete(){
        // 创建索引
        TransportClient client = getClient();
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "user", "1");
        DeleteResponse deleteResponse = deleteRequestBuilder.get();
        System.out.println(deleteResponse);
    }

    @Test
    public void search(){
        // 创建索引
        TransportClient client = getClient();
        // 指定索引库
        SearchRequestBuilder pethome = client.prepareSearch("pethome");
        // 分页的指定列 分页的方法：倒排
        pethome.addSort("age", SortOrder.DESC);
        // 从第几页开始 每页多少数据
        pethome.setFrom(0);
        pethome.setSize(10);

        // 设置一个查询条件:bool
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        // 按key查询
        boolQueryBuilder.must(QueryBuilders.matchQuery("username", "leilei kaikai"));
        boolQueryBuilder.filter(QueryBuilders.termQuery("sex", 1))
                .filter(QueryBuilders.rangeQuery("age").gte(10).lt(50));

        // 装入最终的结果中
        SearchResponse searchResponse = pethome.setQuery(boolQueryBuilder).get();
        SearchHits hits = searchResponse.getHits();
        long totalHits = hits.totalHits();
        System.out.println("总条数 = " + totalHits);

        // 结果列表
        SearchHit[] list = hits.getHits();
        for (SearchHit searchHit : list) {
            Map<String, Object> source = searchHit.getSource();
            System.out.println(source);
        }
    }
}
